﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using UAC;
using UAC.Operation;

namespace UACManager
{
    public partial class DocumentForm : Form
    {
        private int documentDefId;
        public DocumentForm(int documentDefId)
        {
            InitializeComponent();
            this.documentDefId = documentDefId;
        }

        private void DocumentForm_Load(object sender, EventArgs e)
        {
            this.Text = string.Format("文档定义 - {0}", this.documentDefId);
            ListOperationControl();
            ListDocumentFieldDef();
            ListOperationDef();
        }

        #region ListOperationControl
        private List<ControlCondition> conditions;
        private void ListOperationControl()
        {
            conditions = Utils.ListOperationControl(documentDefId);
            groupBoxCondition.Controls.Clear();
            if (conditions != null)
            {
                foreach (ControlCondition condition in conditions)
                {
                    Panel panel = new Panel() { Dock = DockStyle.Top, Size = new Size() { Height = 24 } };
                    TextBox info = new TextBox() { Text = string.Format("[{0}]{1}", condition.ID, condition.Description), Location = new System.Drawing.Point(6, 3), Size = new System.Drawing.Size(168, 21) };
                    Button btnEdit = new Button() { Text = "Edit", Location = new System.Drawing.Point(180, 2), Size = new System.Drawing.Size(40, 23) };
                    btnEdit.Tag = condition;
                    btnEdit.Click += btnEdit_Click;
                    Button btnDel = new Button() { Text = "Del", Location = new System.Drawing.Point(222, 2), Size = new System.Drawing.Size(36, 23) };
                    btnDel.Tag = condition;
                    btnDel.Click += btnDel_Click;
                    panel.Controls.AddRange(new Control[]{ 
                        info,
                        btnEdit,
                        btnDel
                    });
                    groupBoxCondition.Controls.Add(panel);
                }
            }
        }

        private void btnDel_Click(object sender, EventArgs e)
        {
            ControlCondition condition = ((Button)sender).Tag as ControlCondition;

        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            ControlCondition condition = ((Button)sender).Tag as ControlCondition;
            if (condition != null)
            {
                using (ControlConditionForm ccf = new ControlConditionForm(this.documentDefId, condition))
                    if (ccf.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                        ListOperationControl();
            }
        }
        #endregion

        #region ListDocumentFieldDef
        private List<DocumentFieldDef> documentFieldDefs;
        private void ListDocumentFieldDef()
        {
            documentFieldDefs = Utils.ListDocumentFieldDef(documentDefId);
            listBoxFieldDefs.Items.Clear();
            if (documentFieldDefs != null)
            {
                foreach (DocumentFieldDef fieldDef in documentFieldDefs)
                {
                    listBoxFieldDefs.Items.Add(fieldDef);
                }
            }
        }
        #endregion

        #region ListOperationDef
        private List<OperationDef> operationDefs;
        private void ListOperationDef()
        {
            operationDefs = Utils.ListOperationDef(documentDefId);
            groupBoxOperation.Controls.Clear();
            if (operationDefs != null)
            {
                foreach (OperationDef operationDef in operationDefs)
                {
                    AddOperationDef(operationDef);
                }
            }
        }

        private void AddOperationDef(OperationDef operationDef)
        {
            Panel panel = new Panel() { Dock = DockStyle.Top, Size = new Size() { Height = 24 } };
            panel.Controls.AddRange(new Control[]{ 
                        new TextBox() { Text = string.Format("[{0}]{1}", operationDef.OperationDefID, operationDef.Name),Location = new System.Drawing.Point(6, 3),Size= new System.Drawing.Size(160, 21) },
                        new TextBox() { Text = operationDef.OCID.ToString(), Location = new System.Drawing.Point(170, 3), Size = new System.Drawing.Size(50, 21) },
                    });
            groupBoxOperation.Controls.Add(panel);
        }
        #endregion

        private void btnAddOperation_Click(object sender, EventArgs e)
        {
            using (OperationDefForm odf = new OperationDefForm(this.documentDefId))
            {
                if (odf.ShowDialog() == System.Windows.Forms.DialogResult.OK && odf.OperationDef != null)
                {
                    operationDefs.Add(odf.OperationDef);
                    AddOperationDef(odf.OperationDef);
                }
            }
        }

        private void btnAddDocFieldDef_Click(object sender, EventArgs e)
        {
            using (DocFieldDefForm dfdf = new DocFieldDefForm(this.documentDefId))
            {
                if (dfdf.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    ListDocumentFieldDef();
                }
            }
        }
    }
}
